package com.poker.service;

import com.poker.entity.Activity;
import com.poker.entity.ActivityOuts;
import com.poker.entity.ActivityUser;
import com.poker.repository.ActivityUserRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.List;

/**
 * @ClassName ActivityUserService
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/13 13:47
 * @Version 1.0
 **/
@Service
public class ActivityUserService {

    @Resource
    private ActivityUserRepository activityUserRepository;

    public Page<ActivityUser> findListAll(Integer page, Integer size,Integer classifya) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");
        Page<ActivityUser> activity =null;
        if(classifya!=null){
            Specification<ActivityUser> spec = new Specification<ActivityUser>() {        //查询条件构造
                public Predicate toPredicate(Root<ActivityUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    Path<Integer> classify = root.get("classify");
                    Predicate p1 = cb.equal(classify, classifya);
                    Predicate p = cb.and(p1);
                    return p;
                }
            };
            activity = activityUserRepository.findAll(spec,pageable);

        }else{
            activity = activityUserRepository.findAll(pageable);
        }
        for(ActivityUser usr:activity){
            if(usr.getUserIdNumber()!=null&&usr.getUserIdNumber().length()>14){
                StringBuffer sb = new StringBuffer(usr.getUserIdNumber());
                sb.replace(6, 14, "****");
                usr.setUserIdNumber(sb+"");
            }

        }
        return activity;
    }

    public Page<ActivityUser> findList(Integer page, Integer size, ActivityUser activityUser,Integer classifya) {
        Pageable pageable = PageRequest.of(page, size,Sort.Direction.DESC, "createTime");
        Specification<ActivityUser> spec = new Specification<ActivityUser>() {        //查询条件构造
            public Predicate toPredicate(Root<ActivityUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> userName = root.get("userName");
                Predicate p2 = cb.like(userName, "%"+activityUser.getUserName()+"%");
                Path<String> activityName = root.get("activityName");
                Predicate p3 = cb.like(activityName, "%"+activityUser.getActivityName()+"%");
                Predicate p;
                if(classifya!=null){
                    Path<Integer> classify = root.get("classify");
                    Predicate p0 = cb.equal(classify, classifya);
                    p = cb.and(p0,p2);
                }else {
                    p = cb.and(p3,p2);
                }

                return p;
            }
        };
        Page<ActivityUser> activity = activityUserRepository.findAll(spec, pageable);
        for(ActivityUser usr:activity){
            if(usr.getUserIdNumber()!=null&&usr.getUserIdNumber().length()>14){
                StringBuffer sb = new StringBuffer(usr.getUserIdNumber());
                sb.replace(6, 14, "****");
                usr.setUserIdNumber(sb+"");
            }

        }
        return  activity;

    }

    public void updateStatus(String id, Integer status) {
        ActivityUser one = activityUserRepository.getOne(id);
        one.setUserStatus(status);
        if(status==2){
            ActivityUser str = activityUserRepository.findListByActivityId(one.getActivityId());
               if(str!=null){
                   if(str.getUserNumber()!=null){
                       Integer i = Integer.valueOf(str.getUserNumber());
                       String no=String.format("%04d",i+1);
                       one.setUserNumber(no);
                   }
               }else{
                   one.setUserNumber("0001");
               }

        }
        activityUserRepository.saveAndFlush(one);
    }

    public ActivityUser getById(String id) {
        return activityUserRepository.getOne(id);
    }

    public void deleteActivityUser(String id) {
        activityUserRepository.deleteById(id);
    }



}
